package com.saber.thread;

import java.util.Calendar;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class MyThreadPoolExecutor extends ThreadPoolExecutor {
	private long runTime = 0L;

	public MyThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
			BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
		super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
	}

	@Override
	protected void beforeExecute(Thread t, Runnable r){
		Calendar cal = Calendar.getInstance();
		runTime = cal.getTimeInMillis();
		//System.out.println("Thread Pool -> start time:" + runTime);
	}

	@Override
	protected void afterExecute(Runnable r, Throwable t) {
		Calendar cal = Calendar.getInstance();
		runTime = cal.getTimeInMillis();
		//System.out.println("sharedThreadPool-> endTime:" + r.toString() + runTime);
//		+ ", active:" + this.getActiveCount()
//		+ ", pool size:" + this.getPoolSize()
//		+ ", remainnig workqueue:" + this.getQueue().remainingCapacity()
//		+ ", max pool size:" + this.getMaximumPoolSize()
//		+ ", completed task count:" + this.getCompletedTaskCount()
//		+ ", task count:" + this.getTaskCount());
	}

	@Override
	protected void terminated() {
		Calendar cal = Calendar.getInstance();
		runTime = cal.getTimeInMillis() - runTime;
		System.out.println("Thread Pool -> has run time:" + runTime);
	}
}
